package com.webanovare.model;

import com.webanovare.model.db.User;
import com.webanovare.model.db.UserGroup;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author daniel
 */
@Stateless
public class UserRegistry extends AbstractDAO<User, Long> {
    @PersistenceContext(unitName = "webanovare_pu")
    protected EntityManager em;
    
    public UserRegistry() {
        super(User.class);
    }
        
    @PostConstruct
    public void postContruct(){
        Logger.getAnonymousLogger().log(Level.INFO, "Alive {0}", this);
        super.setEntitymanager(em);
    }
    
    public User findUser(String name) {
        User user = em.find(User.class, name);
        return user;
    }
    
    public boolean isInAdminGroup(String userName) {
        User user = em.find(User.class, userName);
        
        for (UserGroup g : user.getGroups()) {
            if (g.equals(UserGroup.ADMIN)) {
                return true;
            }
        }
        return false;
        
    }
}
